package _12_回溯;

import java.util.ArrayList;
import java.util.List;

public class _38_外观数列 {

    public static void main(String[] args) {
        _38_外观数列 v = new _38_外观数列();
        System.out.println(v.countAndSay(4));
    }

    public String countAndSay(int n) {
        if (n == 1) {
            return "1";
        }
        String str = countAndSay(n - 1);
        StringBuilder sb = new StringBuilder();
        char prev = ' ';
        int count = 0;
        int len = str.length();
        for (int i = 0; i < len; i++) {
            char cur = str.charAt(i);
            if (prev != cur) {
                if (prev != ' ') {
                    sb.append(count).append(prev);
                }
                count = 1;
                prev = cur;
            } else {
                count++;
            }
        }
        sb.append(count).append(prev);
        return sb.toString();
    }
}
